package org.confucianismframework.orm.sql.produce.support;

import org.confucianismframework.orm.enums.SQLType;
import org.confucianismframework.orm.pattern.EntityNamePattern;
import org.confucianismframework.orm.annotation.RegisterSQLStrategy;
import org.confucianismframework.orm.sql.produce.SqlProduce;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * 生成简单的 Select 语句 （不带条件）
 *
 * @author zy
 */
@RegisterSQLStrategy(SQLType.SELECT)
public class SelectSqlProduce implements SqlProduce {

    @Override
    public String produce(Object instance) {

        Class<?> instanceClazz = instance.getClass();

        List<Field> fields = Arrays.asList(instanceClazz.getDeclaredFields());

        StringBuilder sql = new StringBuilder(100);

        sql.append("SELECT ");

        for (Field field : fields) {

            String column = field.getName();

            sql.append(EntityNamePattern.humpToLine(column)).append(",");

        }

        sql.deleteCharAt(sql.length() - 1);
        sql.append(" FROM ").append(EntityNamePattern.humpToLine(instanceClazz.getSimpleName()));

        return sql.toString();
    }

}
